package com.weetop.cfw.mine.fragment

import com.blankj.utilcode.util.ToastUtils
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.ui.fragment.CommonBaseFragment
import com.weetop.cfw.bean.MyCoursesBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.callback.RxJavaCallBack
import com.weetop.cfw.mine.adapter.CoursesPurchasedByUsersListAdapter
import com.weetop.cfw.utils.RetrofitUtils
import com.weetop.cfw.utils.RxJavaUtils
import kotlinx.android.synthetic.main.fragment_user_courses.*

/**
 * 支付成功课程订单界面
 */
class SuccessfulPaymentCoursesFragment:CommonBaseFragment(),OnRefreshLoadMoreListener {

    private lateinit var coursesPurchasedByUsersListAdapter: CoursesPurchasedByUsersListAdapter

    private var currentPageNumber = 1//当前数据的页数
    private var isRefreshing = false//是否处在刷新状态
    private var isLoadingMore = false//是否处在加载更多状态
    private var totalCount = 0//数据总条数

    private val successfulPaymentCoursesList = ArrayList<MyCoursesBean.DataBean>()

    override fun getLayoutId() = R.layout.fragment_user_courses

    override fun initView() {

    }

    override fun initData() {
        userCoursesRefreshLayout.setEnableAutoLoadMore(false)
        userCoursesRefreshLayout.setOnRefreshLoadMoreListener(this)

        coursesPurchasedByUsersListAdapter = CoursesPurchasedByUsersListAdapter(R.layout.my_course_item_layout,successfulPaymentCoursesList)
        coursesPurchasedByUsersListAdapter.emptyView = emptyView
        userCoursesRV.adapter = coursesPurchasedByUsersListAdapter
    }

    override fun lazyLoadData() {
        if(isFirstLoadData){
            getSuccessfulPaymentCourses(currentPageNumber)
        }
    }

    /**
     * 获取支付成功的课程订单列表
     */
    private fun getSuccessfulPaymentCourses(tempCurrentPageNumber:Int){
       showLoadingDialog(activity!! as CommonBaseActivity,"正在获取支付成功课程订单列表...")
        RxJavaUtils.useInSupportFragmentOnDestoryView(RetrofitUtils.getApi().getMyCoursesList(status = 1,curpage = tempCurrentPageNumber,pagesize = 10),this,object:RxJavaCallBack<MyCoursesBean>(){

            override fun onSuccess(t: MyCoursesBean?) {
                dismissLoadingDialog()
                if(t != null){
                    isFirstLoadData = false
                    if(isRefreshing){
                        successfulPaymentCoursesList.clear()
                        successfulPaymentCoursesList.addAll(t.data)
                        coursesPurchasedByUsersListAdapter.notifyDataSetChanged()
                        userCoursesRefreshLayout.finishRefresh(true)
                        if(t.data.size > 0){
                            currentPageNumber = 1
                        }
                        isRefreshing = false
                        return
                    }
                    if(isLoadingMore){
                        successfulPaymentCoursesList.addAll(t.data)
                        coursesPurchasedByUsersListAdapter.notifyDataSetChanged()
                        userCoursesRefreshLayout.finishLoadMore(true)
                        if(t.data.size > 0){
                            currentPageNumber += 1
                        }
                        isLoadingMore = false
                        return
                    }
                    successfulPaymentCoursesList.addAll(t.data)
                    coursesPurchasedByUsersListAdapter.notifyDataSetChanged()
                }
            }

            override fun onRequestError(t: MyCoursesBean?) {
                super.onRequestError(t)
                dismissLoadingDialog()
                userCoursesRefreshLayout.finishRefresh(false)
                userCoursesRefreshLayout.finishLoadMore(false)
            }

        })
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(successfulPaymentCoursesList.size >= totalCount){
            ToastUtils.showShort("暂无更多数据")
            userCoursesRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadingMore = true
            getSuccessfulPaymentCourses(currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefreshing = true
        getSuccessfulPaymentCourses(1)
    }
}